package com.baidumap.administrator.happystudy.ui;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

import com.baidumap.administrator.happystudy.base.JsonTools;
import com.baidumap.administrator.happystudy.base.Utils;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.ResaurantHomeTab2Adapter;
import com.baidumap.administrator.happystudy.view.SwipyRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.Map;

public class RestaurantHomeTab2Activity extends Activity implements SwipyRefreshLayout.OnRefreshListener{


    public ImageLoader loader;
    private ListView mListView;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private ResaurantHomeTab2Adapter mAdapter;
    private static int pageNo = 1;

    private SwipyRefreshLayout mSwipyRefreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restaurant_home_tab2);
        iniListView();
    }

    private void iniListView() {
        mSwipyRefreshLayout = (SwipyRefreshLayout) findViewById(R.id.main_SwipyRefreshLayout);
        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(pageNo);
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(this);
        // LoadImage初始化
        loader = ImageLoader.getInstance();
        pageNo = 1;
        mListView = (ListView) findViewById(R.id.list);
        mAdapter = new ResaurantHomeTab2Adapter(RestaurantHomeTab2Activity.this, loader);
        new RestaurantHomeTab2Activity.MyTask().execute("", "" + pageNo);// 线程加载json数据
    }


    class MyTask extends
            AsyncTask<String, Void, ArrayList<Map<String, Object>>> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected ArrayList<Map<String, Object>> doInBackground(
                String... params) {

            ArrayList<Map<String, Object>> mList = null;
            mList = (ArrayList<Map<String, Object>>) JsonTools.listKeyMaps(
                    "list", Utils.HOUSE_JSON);
            return mList;
        }

        protected void onPostExecute(ArrayList<Map<String, Object>> result) {
            // TODO Auto-generated method stub

            list.addAll(result);//

            mAdapter.bindData(list);
            if (pageNo == 1) {

                mListView.setAdapter(mAdapter);
            }
            pageNo++;
            mAdapter.notifyDataSetChanged();
            super.onPostExecute(result);

        }
    }

    /**
     * 下拉刷新
     *
     * @param index 当前分页索引
     */
    @Override
    public void onRefresh(int index) {
        //list.clear();
        //pageNo = 1;

        mSwipyRefreshLayout.setRefreshing(false);
    }

    /**
     * 加载更多
     *
     * @param index 当前分页索引
     */
    @Override
    public void onLoad(int index) {
        mSwipyRefreshLayout.setRefreshing(false);
    }

}
